package logic

import (
	"activity_srv/activity"
	"activity_srv/internal/svc"
	model3 "activity_srv/model"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"homepage_srv/model"
	"smart-health-platforms/pkg"
	"time"
)

type BargainAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBargainAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BargainAddLogic {
	return &BargainAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BargainAddLogic) BargainAdd(in *activity.BargainAddRequest) (*activity.BargainAddResponse, error) {
	var sku model.GoodSku
	err := sku.GetByGoodId(int(in.SkuId))
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		return nil, err
	}
	var good model.Good
	err = good.GetGoodId(int(sku.GoodId))
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		return nil, err
	}
	layout := "2006-01-02 15:04:05"

	// 2. 解析字符串为 time.Time
	staTime, err := time.Parse(layout, in.StrTime)
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		// 处理解析失败（根据业务需求返回错误或设置默认时间）
		return nil, fmt.Errorf("解析 StaTime 失败: %w", err)
	}
	EndTime, err := time.Parse(layout, in.EndTime)
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		// 处理解析失败（根据业务需求返回错误或设置默认时间）
		return nil, fmt.Errorf("解析 StaTime 失败: %w", err)
	}
	ba := model3.Bargain{
		Name:       good.StoreName,
		SkuId:      int32(in.SkuId),
		StaTime:    staTime,
		EndTime:    EndTime,
		FinalPrice: int32(in.FinalPrice),
		People:     int32(in.People),
		Num:        1,
		Price:      sku.Price,
		Status:     1,
	}
	err = ba.AddBargain()
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		return nil, err
	}
	return &activity.BargainAddResponse{
		Message: "成功",
	}, nil
}
